//
//  UITableView+UpdateCellDeleteFrame.m
//  MorninyDiaryB
//
//  Created by bill on 2021/3/22.
//  Copyright © 2021 bill. All rights reserved.
//  Git: https://gitee.com/dosedo/modify-delete-btn-demo
//

#import "UITableView+DeleteButtonFrame.h"


@implementation UITableView (DeleteButtonFrame)

- (void)setupSlideDeleteBtnOriginY:(CGFloat)originY height:(CGFloat)height editingIndexPath:(NSIndexPath*)editingIndexPath{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setupCellDeleteBtnOriginY:originY height:height editingIndexPath:editingIndexPath];
    });
}


#pragma mark - Private

- (void)setupCellDeleteBtnOriginY:(CGFloat)originY height:(CGFloat)height editingIndexPath:(NSIndexPath*)editingIndexPath{
    // 判断系统是否是 iOS13 及以上版本
    if (@available(iOS 13.0, *)) {
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
                // 修改删除按钮
                UIView *remarkContentView = subView.subviews.firstObject;
                [self setupRowActionView:remarkContentView oy:originY h:height];
            }
        }
        return;
    }
    
    // 判断系统是否是 iOS11 及以上版本
    if (@available(iOS 11.0, *)) {
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
                // 修改删除按钮
                UIView *remarkContentView = subView;
                [self setupRowActionView:remarkContentView oy:originY h:height];
            }
        }
        return;
    }
    
    // iOS11 以下的版本
    UITableViewCell *cell = [self cellForRowAtIndexPath:editingIndexPath];
    for (UIView *subView in cell.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1) {
            // 修改尺寸
            UIView *remarkContentView = subView;
            [self setupRowActionView:remarkContentView oy:originY h:height];
        }
    }
}

- (void)setupRowActionView:(UIView*)actionView oy:(CGFloat)oy h:(CGFloat)h {
    CGRect frame = actionView.frame;
    frame.origin.y = oy;
    frame.size.height = h;
    actionView.frame = frame;
}


@end

